<template>
  <div class="user">
    <page-search
      :searchFormConfig="searchFormConfig"
      @resetBtnClick="handleResetBtnClick"
      @searchBtnClick="handleSearchBtnClick"
    />
    <page-content
      :contentTableConfig="contentTableConfig"
      pageName="users"
      ref="pageContentRef"
      @newBtnClick="handleNewData"
      @editBtnClick="handleEditData"
    />
    <page-modal
      :defaultInfo="defaultInfo"
      ref="pageMoadlRef"
      pageName="users"
      :modalConfig="modalConfigRef"
    />
  </div>
</template>

<script lang="ts" setup>
import { computed } from 'vue'
import { useStore } from '@/store'

import pageSearch from '@/components/page-search'
import PageContent from '@/components/page-content'
import PageModal from '@/components/page-modal'

import { searchFormConfig } from './config/searchConfig'
import { contentTableConfig } from './config/contentConfig'
import { modalConfig } from './config/modalConfig'

import { usePageSearch } from '@/hooks/usePageSearch'
import { usePageModal } from '@/hooks/use-page-modal'

const store = useStore()

const { pageContentRef, handleResetBtnClick, handleSearchBtnClick } =
  usePageSearch()

// 处理密码逻辑
const newCallback = () => {
  for (const item of modalConfig.formItems) {
    if (item.field === 'password') item.isHidden = false
  }
}
const editCallback = () => {
  for (const item of modalConfig.formItems) {
    if (item.field === 'password') item.isHidden = true
  }
}

// 动态添加部门和角色列表
const modalConfigRef = computed(() => {
  // const departmentItem = modalConfig.formItems.find(
  //   (item) => item.field === 'departmentId'
  // )
  // if (departmentItem)
  //   departmentItem.options = store.state.entireDepartment.map((item) => {
  //     return { title: item.name, value: item.id }
  //   })
  const roleItem = modalConfig.formItems.find((item) => item.field === 'roleId')
  if (roleItem)
    roleItem.options = store.state.entireRole.map((item) => {
      return { title: item.name, value: item.id }
    })
  return modalConfig
})

const { pageMoadlRef, defaultInfo, handleNewData, handleEditData } =
  usePageModal(newCallback, editCallback)
</script>

<style scoped lang="less"></style>
